<?php

declare (strict_types = 1);

namespace app\service\admin\platform;

use app\model\platform\AgreementModel;
use app\model\platform\FeedbackModel;
use own\auth\User;

class PlatformService
{
    /**
     * 协议或文章列表
     * @param array $param
     */
    public function agreementList(array $param): array
    {
        $map = [];
        if($name = $param['name']??'') {
            $map[] = ['name', 'like', '%'.trim($name).'%'];
        }
        $list = AgreementModel::where($map)
            ->page($param['page'], $param['pageSize'])
            ->order('sort asc')->order('id desc')
            ->select();
        return list_fmt($list, AgreementModel::where($map)->count());
    }
    /**
     * 协议或文章编辑
     * @param array $data
     * @param integer $id
     */
    public function agreementEdit(array $data, int $id)
    {
        $auth = make(User::class);
        if($id) {
            $agreement = AgreementModel::find($id);
            if(!$agreement) {
                tips('未找到相关信息记录，修改失败');
            }
            $agreement->updated_by = $auth->getUser();
        } else {
            $agreement = new AgreementModel();
            $agreement->created_at = $auth->getUser();
        }
        $agreement->name = trim($data['name']);
        $agreement->mark = trim($data['mark']);
        $agreement->content = $data['content'];
        $agreement->sort = $data['sort'];
        return $agreement->save();
    }
    /**
     * 客户反馈列表
     * @param array $param
     */
    public function feedbackList(array $param): array
    {
        $map = [];
        //反馈类型：1咨询 2建议 3其他
        if($feedbackType = $param['feedback_type']??0) {
            $map[] = ['feedback_type', '=', $feedbackType];
        }
        //联系方式
        if($mobile = $param['mobile']??0) {
            $map[] = ['mobile', 'like', '%'.trim($mobile).'%'];
        }
        //问题描述
        if($problem = $param['problem']??0) {
            $map[] = ['problem', 'like', '%'.trim($problem).'%'];
        }
        $list = FeedbackModel::where($map)
            ->page($param['page'], $param['pageSize'])
            ->order('is_read asc')->order('id desc')
            ->select();
        return list_fmt($list, FeedbackModel::where($map)->count());
    }
    /**
     * 客户反馈详情
     * @param integer $id
     */
    public function feedbackInfo(int $id)
    {
        $info = FeedbackModel::find($id);
        if($info) {
            $info->enclosures = explode(',', $info->enclosures);
            if($info->is_read == 0) {
                $info->is_read == 1;
                $info->save();
            }
        }
        return $info;
    }
}